namespace RL

open Raylib_CsLo
open System.Numerics

type SpriteAtlas(tex : Texture, spriteW : int, spriteH : int) =
    let texture = tex
    let spriteWidth = spriteW
    let spriteHeight = spriteH
    let spritesPerRow = texture.width / spriteW
    let spritesPerColumn = texture.height / spriteH
    let maxIndex = spritesPerRow * spritesPerColumn

    let GetRect(index : int) =
        if index > maxIndex || index < 0 then
            Rectangle( single 0,
                      single 0,
                      single spriteWidth,
                      single spriteHeight )
        else
            Rectangle( single((index % spritesPerRow) * spriteWidth),
                      single((index / spritesPerRow) * spriteHeight),
                      single spriteWidth,
                      single spriteHeight )

    member _.SpriteDimensions =
        Vector2(single spriteWidth, single spriteH)

    member s.DrawSprite(index : int, position : Vector2, tint : Color) =
        let pos = position * s.SpriteDimensions
        Raylib.DrawTexturePro( texture,
                               GetRect(index),
                               Rectangle( pos.X,
                                          pos.Y,
                                          single spriteWidth,
                                          single spriteHeight ),
                               Vector2.Zero,
                               single 0,
                               tint )
